#include "threadPool.h"
void *threadFunc(void *arg)
{
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    while (1)
    {
        pthread_mutex_lock(&pthreadPool->taskQueue.mutex);
        while (pthreadPool->taskQueue.size <= 0)
        {
            pthread_cond_wait(&pthreadPool->taskQueue.cond, &pthreadPool->taskQueue.mutex);
        }
        int netfd = pthreadPool->taskQueue.front->net_fd;
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);
        printf("netfd: %d\n", netfd);
        // 验证用户身份
        train_t train;
        bzero(&train, sizeof(train));
        char username[1024] = {0};
        char password[1024] = {0};
        // 用户名
        recv(netfd, &train.len, sizeof(train.len), 0);
        recv(netfd, train.data, train.len, MSG_WAITALL);
        memcpy(username, train.data, train.len);

        // 第一次输入密码
        recv(netfd, &train.len, sizeof(train.len), 0);
        recv(netfd, train.data, train.len, MSG_WAITALL);
        memcpy(password, train.data, train.len);

        int result = passwd_verify(username, password); // verify.c

        while (result != 0)
        {
            train.len = strlen("fail");
            strcpy(train.data, "fail");
            send(netfd, &train, sizeof(train.len) + train.len, MSG_NOSIGNAL);

            // 第n次接收密码
            bzero(&password, sizeof(password));
            bzero(&train, sizeof(train));
            recv(netfd, &train.len, sizeof(train.len), 0);
            recv(netfd, train.data, train.len, MSG_WAITALL);
            memcpy(password, train.data, train.len);
            if (train.len == 0)
            {
                close(netfd);
                break;
            }

            result = passwd_verify(username, password);
        }
        if (result == 0)
        {
            train.len = strlen("success");
            strcpy(train.data, "success");
            send(netfd, &train, sizeof(train.len) + train.len, MSG_NOSIGNAL);
            client_info_t client_info; // 记录用户连接信息
            memset(&client_info, 0, sizeof(client_info));
            client_info.netfd = netfd;
            strcpy(client_info.virtual_global_path, "/");
            strcat(client_info.virtual_global_path, strcat(username, "/"));
            strcpy(client_info.global_path, "/home/");
            strcat(client_info.global_path, username);
            // 接收并处理客户端命令
            handle(&client_info); // 子线程完成任务
            close(netfd);
        }
    }
    return NULL;
}